package com.cdgeekcamp.redas.wechat.component;

import com.cdgeekcamp.redas.wechat.aes.AesException;
import com.cdgeekcamp.redas.wechat.aes.WXBizMsgCrypt;
import com.cdgeekcamp.redas.wechat.config.WeChatConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *对微信消息进行加密和解密处理
 **/
@Component
public class EncryptAndDecryptMessage {

    @Autowired
    private WeChatConfig weChatConfig;

    /**
     *对微信消息进行加密处理
     **/
    public String encryptMessage(String xmlMsg, String timestamp, String nonce) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(
                weChatConfig.getToken(), weChatConfig.getEncodingAESKey(), weChatConfig.getAppId());

        return pc.encryptMsg(xmlMsg, timestamp, nonce);
    }

    /**
     *对微信消息进行解密处理
     **/
    public String decryptMessage(String msgSignature, String timestamp, String nonce, String requestBody) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(
                weChatConfig.getToken(), weChatConfig.getEncodingAESKey(), weChatConfig.getAppId());

        return pc.decryptMsg(msgSignature, timestamp, nonce, requestBody);
    }
}
